package com.ling.zookeeper.zookeeperMain;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ling.zookeeper.zookeeperUtil.BaseZookeeper;
import com.ling.zookeeper.zookeeperUtil.PingUtils;
import com.ling.zookeeper.zookeeperUtil.Uidesin;
import com.ling.zookeeper.zookeeperUtil.ZkUtils;

/**
 * 
 * @author yueling
 * 
 */
public class ZKTest {

	private final static Logger logger = LoggerFactory.getLogger(ZKTest.class);

	public static void main(final String[] args) throws Exception {

		
		Uidesin uidesin = new Uidesin();
		uidesin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 一定要设置关闭
		uidesin.setLocationRelativeTo(null);
		uidesin.infoLabel.setText("请按照提示和样例输入对应的源和目标信息……");
		
		// 响应复制事件
		uidesin.buttonOk.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				BaseZookeeper baseZookeeper = new BaseZookeeper();
				BaseZookeeper destZookeeper = new BaseZookeeper();
				if (PingUtils.pingServer(uidesin.srcZkText.getText().trim())) {
					if (PingUtils.pingServer(uidesin.destZkText.getText().trim())) {
						try {
							baseZookeeper.connectZookeeper(uidesin.srcZkText.getText().trim());
							destZookeeper.connectZookeeper(uidesin.destZkText.getText().trim());
							logger.info("--------connect zookeeper success-----------");
							uidesin.infoLabel.setText("--------connect zookeeper success-----------");
							uidesin.infoLabel.setText(ZkUtils.zkCopyNodesInit(uidesin.srcNodeText.getText().trim(),
									uidesin.destNodeText.getText().trim(), baseZookeeper, destZookeeper));
						} catch (Exception e) {
							uidesin.infoLabel.setText("connect zookeeper failed !check ip:port");
							e.printStackTrace();
						}

						try {
							baseZookeeper.closeConnect();
							destZookeeper.closeConnect();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					} else {
						uidesin.infoLabel.setText("ping 【" + uidesin.destZkText.getText().trim().split(":")[0] + "】 failed!check ip:port");
					}
				} else {
					uidesin.infoLabel.setText("ping 【" + uidesin.srcZkText.getText().trim().split(":")[0] + "】 failed!check ip:port");
				}
			}
		});

		// 响应删除按钮事件
		uidesin.buttondelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				BaseZookeeper baseZookeeper = new BaseZookeeper();
				if (PingUtils.pingServer(uidesin.zkText.getText().trim())) {
					try {
						baseZookeeper.connectZookeeper(uidesin.zkText.getText().trim());
						logger.info("--------connect zookeeper success-----------");
						uidesin.infoLabel.setText("--------connect zookeeper success-----------");

						uidesin.infoLabel
								.setText(ZkUtils.zkRmNodeInit(uidesin.zkNodeText.getText(), baseZookeeper));
						baseZookeeper.closeConnect();
					} catch (Exception e) {
						uidesin.infoLabel.setText("connect zookeeper failed !check ip:port");
						e.printStackTrace();
					}
				} else {
					uidesin.infoLabel.setText("ping 【" + uidesin.zkNodeText.getText().trim().split(":")[0] + "】 failed!check ip:port");
				}

			}
		});
		//响应创建按钮事件
		uidesin.buttonCreate.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				BaseZookeeper baseZookeeper = new BaseZookeeper();
				if (PingUtils.pingServer(uidesin.zkText.getText().trim())) {
					try {
						baseZookeeper.connectZookeeper(uidesin.zkText.getText().trim());
						logger.info("--------connect zookeeper success-----------");
						uidesin.infoLabel.setText("--------connect zookeeper success-----------");

						uidesin.infoLabel
								.setText(ZkUtils.zkCreateNodeInit(uidesin.zkNodeText.getText(),uidesin.nodeData.getText(),baseZookeeper));
						baseZookeeper.closeConnect();
					} catch (Exception e) {
						uidesin.infoLabel.setText("connect zookeeper failed !check ip:port");
						e.printStackTrace();
					}
				} else {
					uidesin.infoLabel.setText("ping 【" + uidesin.zkNodeText.getText().trim().split(":")[0] + "】 failed!check ip:port");
				}

			}
		});
		//响应查询按钮事件
		uidesin.buttonQuery.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				BaseZookeeper baseZookeeper = new BaseZookeeper();
				if (PingUtils.pingServer(uidesin.zkText.getText().trim())) {
					try {
						baseZookeeper.connectZookeeper(uidesin.zkText.getText().trim());
						logger.info("--------connect zookeeper success-----------");
						uidesin.infoLabel.setText("--------connect zookeeper success-----------");

						uidesin.infoLabel
								.setText(ZkUtils.zkQueryNodeInit(uidesin.zkNodeText.getText(), baseZookeeper));
						baseZookeeper.closeConnect();
					} catch (Exception e) {
						uidesin.infoLabel.setText("connect zookeeper failed !check ip:port");
						e.printStackTrace();
					}
				} else {
					uidesin.infoLabel.setText("ping 【" + uidesin.zkNodeText.getText().trim().split(":")[0] + "】 failed!check ip:port");
				}

			}
		});
		
		addDocumentEvent(uidesin.srcZkText,uidesin.srcZkResultlabel,"ip");
		addDocumentEvent(uidesin.destZkText,uidesin.destZkResultlabel,"ip");
		addDocumentEvent(uidesin.zkText,uidesin.deleteZkResultlabel,"ip");
		
		addDocumentEvent(uidesin.srcNodeText,uidesin.srcNodeResultLabel,"path");
		addDocumentEvent(uidesin.destNodeText,uidesin.destNodeResultLabel,"path");
		addDocumentEvent(uidesin.zkNodeText,uidesin.deleteNodeResultLabel,"path");
		
		uidesin.setResizable(false);
		uidesin.setVisible(true);
	}
	
	private static void addDocumentEvent(JTextField jTextField,JLabel jLabel,String flag){
		jTextField.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				if (flag.toUpperCase().equals("IP")) {
					if(PingUtils.ipCheck(jTextField.getText().trim())){
						jLabel.setText("√");
						jLabel.setForeground(Color.green);
					}else {
						jLabel.setText("×");
						jLabel.setForeground(Color.red);
					}
				}else if (flag.toUpperCase().equals("PATH")) {
					if(PingUtils.pathCheck(jTextField.getText().trim())){
						jLabel.setText("√");
						jLabel.setForeground(Color.green);
					}else {
						jLabel.setText("×");
						jLabel.setForeground(Color.red);
					}
				}
				
			}
			
			@Override
			public void insertUpdate(DocumentEvent e) {
				if (flag.toUpperCase().equals("IP")) {
					if(PingUtils.ipCheck(jTextField.getText().trim())){
						jLabel.setText("√");
						jLabel.setForeground(Color.green);
					}else {
						jLabel.setText("×");
						jLabel.setForeground(Color.red);
					}
				}else if (flag.toUpperCase().equals("PATH")) {
					if(PingUtils.pathCheck(jTextField.getText().trim())){
						jLabel.setText("√");
						jLabel.setForeground(Color.green);
					}else {
						jLabel.setText("×");
						jLabel.setForeground(Color.red);
					}
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent e) {
				if (flag.toUpperCase().equals("IP")) {
					if(PingUtils.ipCheck(jTextField.getText().trim())){
						jLabel.setText("√");
						jLabel.setForeground(Color.green);
					}else {
						jLabel.setText("×");
						jLabel.setForeground(Color.red);
					}
				}else if (flag.toUpperCase().equals("PATH")) {
					if(PingUtils.pathCheck(jTextField.getText().trim())){
						jLabel.setText("√");
						jLabel.setForeground(Color.green);
					}else {
						jLabel.setText("×");
						jLabel.setForeground(Color.red);
					}
				}
			}
		});
	}
}
